//
//  CYMyDefaultRecordsVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/6/12.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYMyDefaultRecordsVController.h"
#import "CYABTool.h"
#import "CYSimpleRecord.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#import "CYABDetailVController.h"
#import "CYMyDefaultCell.h"
#import "CYTool.h"
#import "CYCreditDetailVC.h"


@interface CYMyDefaultRecordsVController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *datas;
@end

@implementation CYMyDefaultRecordsVController

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [CYTool setExtraCellLineHiddenWithTableView:self.myTableView];
    //加载本地数据
    [self loadData];
}

- (void)loadData {
    NSUserDefaults *deft = [NSUserDefaults standardUserDefaults];
    NSArray *simpleDics = [deft objectForKey:CYSimpleRecordsDicKey];
    for (NSDictionary *dic in simpleDics) {
        CYSimpleRecord *simple = [CYSimpleRecord objectWithKeyValues:dic];
        CYPerson *person = [self setupPerson:simple];
        [self.datas addObject:person];
    }
    if (!self.datas.count) {
        UIView *cover = [[UIView alloc] initWithFrame:self.view.frame];
        [self.view addSubview:cover];
        //添加约束
        [cover autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
        [cover autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:64];
        
        cover.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initForAutoLayout];
        [cover addSubview:label];
        [label autoCenterInSuperview];
        label.text = @"没有更多数据";
    }
}

- (CYPerson *)setupPerson:(CYSimpleRecord *)simpRec {
    CYPerson *cyperson = [[CYPerson alloc] init];
    
    cyperson.simpleRecord = simpRec;
    cyperson.person = nil;
    cyperson.pushTag = 1;
    cyperson.segmentTag = 1;
    return cyperson;
}

#pragma mark - tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CYMyDefaultCell *cell = [CYMyDefaultCell cellWithTableView:tableView];
    cell.person = self.datas[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CYPerson *cyperson = self.datas[indexPath.row];
//    CYABDetailVController *controll = [[CYABDetailVController alloc] init];
//    controll.person = cyperson;
//    controll.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:controll animated:YES];
    //违约详情
    CYCreditDetailVC *credetail = [[CYCreditDetailVC alloc] init];
    CYDefaultRecordAO *defr = [CYDefaultRecordAO new];
    defr.defaultRecordId = cyperson.simpleRecord.recordId;
    defr.type = @"0";
    defr.pushTag = 1;
    credetail.defr = defr;
    [self.navigationController pushViewController:credetail animated:YES];
}

- (void)dealloc {
    CYLog(@"我的失信人销毁");
}

@end
